package com.lxy.config;


import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = {MinioProperties.class})
public class MinioConfig {

    @Autowired
    MinioProperties minioProperties;



    @Bean
    public MinioClient minioClient() throws MinioException {
        String endpoint = minioProperties.getEndpoint();
        String accessKey = minioProperties.getAccessKey();
        String secretKey = minioProperties.getSecretKey();
        String bucketName = minioProperties.getBucketName();
        if (endpoint != null && !"".equals(endpoint)) {
            if (accessKey != null && !"".equals(accessKey)) {
                if (secretKey != null && !"".equals(secretKey)) {
                    if (bucketName != null && !"".equals(bucketName)) {
                        MinioClient minioClient = MinioClient.builder()
                                .endpoint(endpoint)
                                .credentials(accessKey, secretKey)
                                .build();
                        this.makeBucket(minioClient, bucketName);
                        return minioClient;
                    } else {
                        throw new MinioException("存储桶名称未在application.yml配置！");
                    }
                } else {
                    throw new MinioException("Minio密码未在application.yml配置！");
                }
            } else {
                throw new MinioException("Minio用户名未在application.yml配置！");
            }
        } else {
            throw new MinioException("Minio的URL未在application.yml配置！");
        }

    }

    private void makeBucket(MinioClient minioClient, String bucketName) throws MinioException {
        try {
            boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!isExist) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                String policyJson = "{"
                        + "\"Version\":\"2012-10-17\","
                        + "\"Statement\":["
                        + "{"
                        + "\"Effect\":\"Allow\","
                        + "\"Principal\":\"*\","
                        + "\"Action\":[\"s3:GetObject\"],"
                        + "\"Resource\":[\"arn:aws:s3:::" + bucketName + "/*\"]"
                        + "}"
                        + "]"
                        + "}";
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                        .bucket(bucketName)
                        .config(policyJson)
                        .build());
            }
        } catch (Exception e) {
            throw new MinioException("创建minio存储桶异常"+e.getMessage());
        }
    }
}
